package com.spring.housing.service.loginService;

import com.alibaba.fastjson.JSON;
import com.netflix.client.http.HttpRequest;
import com.spring.housing.acceptType.PageParams;
import com.spring.housing.constant.Constants;
import com.spring.housing.returnType.CommenResult;
import com.spring.housing.util.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.EAN;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.trace.http.HttpTrace;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.jdbc.core.JdbcTemplate;

import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Slf4j
@Service
public class LonginServer {
    @Autowired
    JdbcTemplate jdbcTemplate;
    @Autowired
    RedisTemplate<String,Object> redisTemplate;
    public CommenResult getinfo(@RequestBody PageParams params){
        CommenResult commenResult = new CommenResult();
        //账户密码登录
        if(params.getParam().get("userName")!=null){
        String sql=" select DISTINCT*  from user  join (select a.location_x,a.location_y,a.house_id,a.house_main from area_house a )a on  a.house_id like concat(user.adress,'%')      where userName = ? && passWord=? GROUP BY userName";
        List<Map<String, Object>> username1 = jdbcTemplate.queryForList(sql,params.getParam().get("userName").toString(),params.getParam().get("passWord").toString());
        if(username1.size()!=0){
            Constants.LOGINSESSION=params.getParam().get("userName").toString();
            Constants.LOGINSESSIONPASS=params.getParam().get("passWord").toString();
            redisTemplate.opsForValue().set(Constants.LOGINSESSION,Constants.LOGINSESSIONPASS,125, TimeUnit.MINUTES);
            //保存用户信息在Redis里
            redisTemplate.opsForValue().set(Constants.UserINFO,username1,125, TimeUnit.MINUTES);
            commenResult.setStatus(200);
            commenResult.setMessage("登录接口调用成功!");


            commenResult.setData(username1);


            return commenResult;
        }
        if(Integer.parseInt(jdbcTemplate.queryForList("select count(*) as res from user where userName = ? ",params.getParam().get("userName").toString()).get(0).get("res").toString())==0)
        {
            commenResult.setStatus(201);
            commenResult.setMessage("查询用户失败,账户未注册!");
            commenResult.setData("登录操作");
            return commenResult;
        }

            commenResult.setStatus(202);
            commenResult.setMessage("查询用户失败,账户/密码出错!");
            commenResult.setData("登录操作");

        }
        //手机号登录
        else{
            commenResult.setStatus(500);
            commenResult.setMessage("服务暂不支持!");
            commenResult.setData("登录操作");
        }
    return commenResult ;
    }

    public String logintest() {
        return  "redisTemplate.opsForValue().get(Constants.LOGINSESSION).toString()";
    }

    public CommenResult<String> register(@RequestBody PageParams params){
       List<Map<String, Object>> map = jdbcTemplate.queryForList("select userName from user where userName=?", new Object[]{params.getParam().get("userName")});
        if(map.size()!=0){
            return  new CommenResult<>(201,"HousingDeliver接口调用成功!","账号已注册");
        }
        jdbcTemplate.update("insert into user(userName,passWord,sex,phone,call) values(?,?,?,?,?)",params.getParam().get("userName"),params.getParam().get("passWord"),params.getParam().get("sex"),params.getParam().get("phone"),params.getParam().get("call"));
    return  new CommenResult<>(200,"HousingDeliver接口调用成功！","账号注册成功!");
    }
}
